home *** CD-ROM | disk | FTP | other *** search
- /*
- File: AGSupprt.cpp
-
- Contains: xxx put contents here xxx
-
- Owned by: Yan Arrouye
-
- Copyright: © 1995-1996 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <6> 9/16/96 eeh 1386008: back to Gestalt on 68K
- <5> 9/12/96 eeh 1386008: weak link against appleguidelib
- <4> 9/11/96 eeh 1386008: AG fix for 68K
- <3> 9/10/96 eeh 1386008: AppleGuide support (incomplete)
- <2> 7/8/96 eeh undo task 10008 (AppleGuide buttons)
- <7> 5/16/96 Yan Style check
- <6> 5/09/96 Yan Fixed leak
- <5> 5/08/96 Yan fixed momory leaks
- <4> 4/25/96 Yan Use CopyPascalString instead of BlockMoveData
- <3> 2/20/96 tb Placed #if qDebug around TraceMonitor support.
- <2> 10/26/95 Yan • Added support for CodeWarrior. Requires that you rename the
- AppleGuideGlue.xcoff file to AppleguideGlueLib to link.
- 8/31/95 Yan Moved to ShareWare
-
- To Do:
- */
-
-
-
- #ifndef _AGSUPPORT_
- #include "AGSupprt.h"
- #endif
-
- #ifndef _DLGDEFS_
- #include "DdgDefs.h"
- #endif
-
- #ifndef _TEMPOBJ_
- #include "TempObj.h"
- #endif
-
- #ifndef _USERSRCM_
- #include <UseRsrcM.h>
- #endif
-
- #ifndef __CONTROLS__
- #include "Controls.h"
- #endif
-
- #ifndef __ICONS__
- #include "Icons.h"
- #endif
-
- #ifndef _INFODEFS_
- #include "InfoDefs.h"
- #endif
-
- //#include "Utilities.h"
-
- #if qDebug
- #include "TraceMonitor.h"
- #define kModuleChannel 'GUID'
- #endif
-
- #include <AppleGuide.h>
- #include <Balloons.h>
-
- #ifndef __GESTALT__
- #include <Gestalt.h>
- #endif
-
- #include <Folders.h>
- #include <Files.h>
- #include <Menus.h>
- #include <TextUtils.h>
-
-
-
- #if GENERATING68K
- #pragma segment AppleGuide
- #endif
-
-
- /*******************************************************************************
- ** Globals
- *******************************************************************************/
-
- #if PRAGMA_ALIGN_SUPPORTED
- #pragma options align=mac68k
- #endif
-
- // Runtime structure
- typedef struct
- {
- FSSpec spec;
- AGRefNum ref;
- } InstalledGuideRec;
-
- typedef struct
- {
- short count;
- InstalledGuideRec guide[1];
- } InstalledGuideList, **InstalledGuideListHdl;
-
-
- // Resources
- typedef struct {
- Str32 name;
- char filler;
- Str255 itemTitle;
- } GuideEntry;
-
-
- typedef struct {
- short count;
- GuideEntry guide[1];
- } GuideList, **GuideListHdl;
-
-
-
- #if PRAGMA_ALIGN_SUPPORTED
- #pragma options align=reset
- #endif
-
-
- InstalledGuideListHdl gInstalledGuidesList = NULL;
- short gInstalledGuidesListSize = 0;
- short gFirstAppleGuideItem = 0;
- Boolean gAGInstallAttempted = false;
- Boolean gAGInstallSucceeded = false;
- FSSpec gFileSpec = { 0, 0, "\p" };
- unsigned long gRefNum = 0;
-
- // forward decl's
- static Boolean FindAppleGuideFolder( short *vol, long *dir );
- static void Plot( ControlHandle ctrlH, IconTransformType trans );
- static void InstallAppleGuide();
-
- /*******************************************************************************
- ** Boolean IsAppleGuidePresent
- *******************************************************************************/
-
- #ifdef _APPLEGUIDE_READY_
- Boolean IsAppleGuidePresent()
- {
- // no point in using Gestalt any longer on PPC. It can only tell whether the
- // shared library was in the Extensions folder at boot time, not whether
- // it's been removed or added since - which is what really determines
- // whether we'll crash trying to call it. (On 68K with Gestalt, though you
- // get a "false positive" in the case where the user's removed the library
- // from his extensions folder since boot, the AppleGuide code handles that
- // very nicely with an error message rather than crashing.)
-
- #if GENERATING68K
- long result = 0;
- OSErr err;
- err = Gestalt(gestaltHelpMgrAttr, &result);
-
- Boolean present = ( err == noErr &&
- (result & (1 << gestaltAppleGuidePresent)) != 0 );
- #elif GENERATINGPOWERPC
- Boolean present = &AGOpenWithSequence != NULL;
- #endif
- return present;
- }
- #endif
-
-
- /*******************************************************************************
- ** Boolean IsAppleGuideInstalled
- *******************************************************************************/
-
- #if 0
- // not being used right now.
- /*
- Boolean IsAppleGuideInstalled()
- {
-
- return IsAppleGuidePresent() && gInstalledGuidesListSize != 0;
- }
- */
- #endif
-
- /*******************************************************************************
- ** void InstallGuide
- *******************************************************************************/
-
- static Boolean FindAppleGuideFolder( short *vol, long *dir )
- {
- WASSERT( IsAppleGuidePresent() );
- Boolean result = false;
- CInfoPBRec pb;
- memset (&pb,0,sizeof(pb));
-
- //First find Extensions Folder
- OSErr err = FindFolder( kOnSystemDisk, kExtensionFolderType,
- kDontCreateFolder, &pb.dirInfo.ioVRefNum, &pb.dirInfo.ioDrDirID );
- if ( err == noErr )
- {
- // Then find global guide folder within in. Name stored in
- // resource for localizability.
- Str255 folderName;
- // this can throw, but that should only be possible when a resource
- // is missing (or low mem, I suppose, but would hope our preflight
- // leaves enough room.) Thus I'm going to let the error cascade
- // on up.
- ODGetIndString( folderName, kODShellGuideFilenameStrings,
- kODShellGuideFolderNameIndex );
-
- pb.dirInfo.ioNamePtr = folderName;
- pb.dirInfo.ioFDirIndex = 0;
- err = PBGetCatInfoSync( &pb );
- if ( err == noErr )
- {
- *vol = pb.dirInfo.ioVRefNum;
- *dir = pb.dirInfo.ioDrDirID;
- result = true;
- }
- }
- return result;
- }
-
- #ifdef _APPLEGUIDE_READY_
- static void InstallAppleGuide()
- {
- if ( !gAGInstallAttempted && IsAppleGuidePresent() )
- {
- gAGInstallAttempted = kODTrue; // only try once -- so we aren't always failing
-
- short vol;
- long dir;
- if ( FindAppleGuideFolder( &vol, &dir ) )
- {
- // Just saving a reference (FSSpec) to the shell guide file
- // -- whose name is in a resource. AppleGuide itself should
- // take care of the other files. Devon says it should take
- // care of the Shell Guide file too (so that I don't have to
- // pass in the FSSpec) but that doesn't work.
-
- Str255 guideFileName;
- ODGetIndString( guideFileName, kODShellGuideFilenameStrings,
- kODShellGuideFileNameIndex );
-
- OSErr err = FSMakeFSSpec( vol, dir, guideFileName, &gFileSpec );
- // enable the dialog help buttons only if the Shell Guide file was
- // found. Other files (e.g. those provided by part developers) will
- // still be available through the help menu, but not this way. Since
- // users expect to find help *about* the dialogs through those buttons
- // and that help is not available, I think this makes the most sense.
- gAGInstallSucceeded = err == noErr;
- }
- }
- #if qDebug
- else
- {
- Trace(kModuleChannel, kFatalMessageLevel, "•• AppleGuide not installed\n");
- }
- #endif
- }
- #endif
-
-
-
- /*******************************************************************************
- ** void CloseAppleGuide
- *******************************************************************************/
-
- #ifdef _APPLEGUIDE_READY_
- void CloseAppleGuide()
- {
- if ( IsAppleGuidePresent() )
- {
- short i;
-
- for ( i = 0; i < gInstalledGuidesListSize; i++ )
- {
- AGRefNum ref = (**gInstalledGuidesList).guide[i].ref;
-
- if ( ref != 0 && AGIsDatabaseOpen(ref) == true )
- {
- // Guide already open
- AGClose(&ref);
- (**gInstalledGuidesList).guide[i].ref = 0;
- }
- }
-
- if ( gInstalledGuidesList )
- {
- DisposeHandle((Handle)gInstalledGuidesList);
- gInstalledGuidesList = NULL;
- }
- AGQuit();
- }
- #if qDebug
- else
- {
- Trace(kModuleChannel, kFatalMessageLevel, "•• AppleGuide not installed\n");
- }
- #endif
- }
- #endif
-
-
-
- /*******************************************************************************
- ** Boolean OpenThisGuide
- *******************************************************************************/
-
- #ifdef _APPLEGUIDE_READY_
- static Boolean OpenThisGuide(FSSpec* guide, AGRefNum* refNum,
- // StringPtr keyword = NULL)
- short sequenceID = 0 )
- {
- WASSERT( IsAppleGuidePresent() );
- Boolean guideOpened = false;
-
- if ( IsAppleGuidePresent() )
- {
- if ( AGIsDatabaseOpen(*refNum) == true )
- {
- // Guide already open
- AGClose(refNum);
- *refNum = 0;
- }
-
- #if qDebug
- Trace(kModuleChannel, kFatalMessageLevel,
- "•• Looking for keyword '%*.*s'\n", keyword[0], keyword[0],
- keyword+1);
- #endif
-
- // Open AppleGuide
- if ( guide == NULL || guide->name[0] != 0 )
- {
- AGErr err;
-
- // Open AppleGuide
- // if ( keyword != NULL && keyword[0] != 0 )
- if ( sequenceID != 0 )
- {
- // Open using the specified keyword
- #if 0
- err = AGOpenWithSearch( guide, 0, NULL, keyword, refNum);
- #else
- err = AGOpenWithSequence( guide, 0, NULL, sequenceID, refNum );
- #endif
- }
- else
- {
- // Open root window
- DebugStr( "\pcalling AGOpenWithView" );
- err = AGOpenWithView( guide, 0, NULL, kAGViewFullHowdy, refNum);
- }
-
- if ( err == noErr )
- {
- guideOpened = true;
- }
- #if qDebug
- else
- {
- Trace(kModuleChannel, kFatalMessageLevel, "••• AppleGuide opening error %d\n", err);
- }
- #endif
- }
- #if qDebug
- else
- {
- Trace(kModuleChannel, kFatalMessageLevel, "•• No guide spec!\n");
- }
- #endif
- }
- #if qDebug
- else
- {
- Trace(kModuleChannel, kFatalMessageLevel, "•• AppleGuide not installed\n");
- }
- #endif
-
- return guideOpened;
- }
- #endif
-
- /*******************************************************************************
- ** Boolean OpenAppleGuide
- *******************************************************************************/
-
- #ifdef _APPLEGUIDE_READY_
- //Boolean OpenAppleGuide(StringPtr keyword)
- Boolean OpenAppleGuide( short sequenceID )
- {
- WASSERT( IsAppleGuidePresent() );
- Boolean opened = false;
-
- if ( true || (gInstalledGuidesListSize > 0) )//|| (gFileSpec.name[0] != 0) )
- {
- TempODHandleLock list((Handle)gInstalledGuidesList);
-
-
- // opened = OpenThisGuide( kAGDefault, &gRefNum, keyword );
- // opened = OpenThisGuide( &gFileSpec, &gRefNum, keyword );
- opened = OpenThisGuide( &gFileSpec, &gRefNum, sequenceID );
-
- // opened = OpenThisGuide( &(**gInstalledGuidesList).guide[0].spec,
- // &(**gInstalledGuidesList).guide[0].ref, keyword);
- }
- return opened;
- }
- #endif
-
- /*******************************************************************************
- ** InitAppleGuideSupport
- *******************************************************************************/
- //short gAGButtonItem = -1;
- ControlDefUPP gCtrlDefUPP = NULL;
- Handle ctrlDefStub = NULL;
- Handle gIconSuite = NULL;
-
- const short kControlInactive = 255;
- const short kControlActive = 0;
- const short kCDEFAddrOffset = 8;
-
- pascal long AGBUTTONCDEF( short code, ControlHandle ctrlH,
- short message, long param );
-
- #ifdef _APPLEGUIDE_READY_
- void InitAppleGuideSupport()
- {
- CUsingLibraryResources r;
- InstallAppleGuide(); // returns immediately if installed
-
- // Must fix up the CDEF resource whether the install succeeded
- // or not; else we'll jsr to 0.
- if ( !gCtrlDefUPP )
- {
- gCtrlDefUPP = NewControlDefProc( AGBUTTONCDEF );
- ctrlDefStub = Get1Resource( 'CDEF', kAGButtonCDEFId);
- WASSERT( ctrlDefStub != NULL );
- (*(ControlDefUPP*)&((*ctrlDefStub)[kCDEFAddrOffset])) = gCtrlDefUPP;
- }
- }
- #endif
-
- /*******************************************************************************
- ** ODGetIndShort
- A utility function patterned after ODGetIndString that might well belong
- elsewhere. Note that it assumes a resource of type 'int#', which follows
- 'STR#' in being a 1-based array of shorts whose first short gives the length
- of the array.
- *******************************************************************************/
-
- short ODGetIndShort( short id, short index )
- {
- // I expect this not to get called, as the control won't generate hits
- // in the dialogs.
- WASSERT( IsAppleGuidePresent() );
-
- short result = 0;
- ODSLong savedRef = BeginUsingLibraryResources();
- Handle h = Get1Resource( 'int#', id );
- OSErr err = ResError();
- if( h ) {
- #if ODDebug
- if( index<1 || index>**(short**)h ) {
- WARN("ODGetIndString(%hd,%hd): invalid index",id,index);
- err= kODErrValueOutOfRange;
- } else
- #endif
- result = ((short*)*h)[index]; // that it's 1-based makes skipping
- // the count easy
- ReleaseResource(h);
- }
- EndUsingLibraryResources(savedRef);
- if( err ) {
- THROW(err);
- THROW(resNotFound);
- }
- return result;
- }
-
- /*******************************************************************************
- ** TakedownAppleGuideSupport
- *******************************************************************************/
-
- #ifdef _APPLEGUIDE_READY_
- void TakedownAppleGuideSupport()
- {
- if ( gCtrlDefUPP )
- {
- DisposeRoutineDescriptor( gCtrlDefUPP );
- gCtrlDefUPP = NULL;
- }
- if ( ctrlDefStub )
- {
- CUsingLibraryResources r; // <eeh> find out if necessary
- ReleaseResource( ctrlDefStub );
- ctrlDefStub = NULL;
- }
- if ( gIconSuite )
- {
- DisposeIconSuite( gIconSuite, true );
- gIconSuite = NULL;
- }
- CloseAppleGuide();
- }
- #endif
-
- /*******************************************************************************
- ** DialogSetUpAppleGuide
- *******************************************************************************/
-
- #ifdef _APPLEGUIDE_READY_
- void DialogSetUpAppleGuide( DialogPtr dlg, short item )
- {
- short itemType;
- ControlHandle cntrlHandle;
- Rect scratchRect;
- GetDialogItem(dlg, item, &itemType, (Handle*)&cntrlHandle,
- &scratchRect );
-
- HiliteControl( cntrlHandle,
- gAGInstallSucceeded ? kControlActive : kControlInactive);
-
- if ( gIconSuite == NULL )
- {
- CUsingLibraryResources r;
- short iconsID = (*cntrlHandle)->contrlRfCon;
- // ignore error; gIconSuite *should* remain null.
- OSErr err = GetIconSuite( &gIconSuite, iconsID,
- kSelectorAllSmallData );
- if ( err != noErr )
- {
- gIconSuite = NULL;
- WARN( "GetIconSuite returned null" );
- }
- }
- }
- #endif
-
- /*******************************************************************************
- ** Plot
- *******************************************************************************/
-
- IconTransformType gCurrentTransform = kTransformNone;
-
- static void Plot( ControlHandle ctrlH, IconTransformType trans )
- {
- if ( gIconSuite )
- {
- OSErr err = PlotIconSuite( &(*ctrlH)->contrlRect,
- kAlignNone, gCurrentTransform=trans,
- (Handle)gIconSuite );
- WASSERT( err == noErr );
- }
- }
-
- /*******************************************************************************
- ** AGBUTTONCDEF
- *******************************************************************************/
-
- pascal long AGBUTTONCDEF( short code, ControlHandle ctrlH,
- short message, long param )
- {
- long result = 0;
- switch ( message )
- {
- case drawCntl:
- if ( (*ctrlH)->contrlVis /* == 255 visible */ )
- {
- short activeCode = (*ctrlH)->contrlHilite;
- switch ( activeCode )
- {
- case kControlInactivePart:
- Plot( ctrlH, kTransformDisabled );
- break;
- case kControlButtonPart:
- WASSERT( IsAppleGuidePresent() );
- Plot( ctrlH, kTransformSelected );
- break;
- default:
- Plot( ctrlH, kTransformNone );
- }
- }
- break;
- case testCntl:
- WASSERT( IsAppleGuidePresent() );
- Point pt = *(Point*)¶m;
- if ( PtInRect( pt, &(*ctrlH)->contrlRect ) )
- {
- result = kControlButtonPart;
- if ( gCurrentTransform == kTransformNone )
- Plot( ctrlH, kTransformSelected );
- }
- break;
-
- // we could use these rather than the methods above to do ifam init
- // case initCntl:
- // case dispCntl:
- // break;
-
- // case calcCRgns: // not an issue for OpenDoc
- case calcCntlRgn:
- param &= 0x7fffffff; // clear high bit (NIM 5-112)
- RectRgn( (RgnHandle)param, &(*ctrlH)->contrlRect );
- break;
-
- }
- return result;
- }
-